package com.easylive.web.controller;

import com.easylive.annotation.RecordUserMessage;
import com.easylive.entity.enums.MessageTypeEnum;
import com.easylive.web.annotation.GlobalInterceptor;
import com.easylive.entity.constants.Constants;
import com.easylive.entity.dto.TokenUserInfoDto;
import com.easylive.entity.enums.ResponseCodeEnum;
import com.easylive.entity.enums.UserActionTypeEnum;
import com.easylive.entity.enums.VideoTopTypeEnum;
import com.easylive.entity.po.UserAction;
import com.easylive.entity.po.VideoComment;
import com.easylive.entity.po.VideoInfo;
import com.easylive.entity.query.UserActionQuery;
import com.easylive.entity.query.VideoCommentQuery;
import com.easylive.entity.request.videocomment.WebVideoCommentLoadCommentReqVO;
import com.easylive.entity.request.videocomment.WebVideoCommentPostCommentReqVO;
import com.easylive.entity.vo.PaginationResultVO;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.entity.vo.VideoCommentResultVO;
import com.easylive.exception.BusinessException;
import com.easylive.service.UserActionService;
import com.easylive.service.VideoCommentService;
import com.easylive.service.VideoInfoService;
import com.easylive.utils.StringTools;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: yudaxian
 * @DateTime: 2024/11/25 21:14
 **/
@RestController
@RequestMapping("/comment")
public class VideoCommentController extends ABaseController {

    @Resource
    private VideoCommentService videoCommentService;

    @Resource
    private VideoInfoService videoInfoService;

    @Resource
    private UserActionService userActionService;

    @PostMapping("/postComment")
    @GlobalInterceptor(checkLogin = true)
    @RecordUserMessage(messageType = MessageTypeEnum.COMMENT)
    public ResponseVO postComment(@Validated WebVideoCommentPostCommentReqVO reqVO) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        VideoComment videoComment = new VideoComment();
        videoComment.setVideoId(reqVO.getVideoId());
        videoComment.setContent(reqVO.getContent());
        videoComment.setImgPath(reqVO.getImgPath());

        videoComment.setUserId(tokenUserInfoDto.getUserId());
        // 需要将这两个返回给前端展示
        videoComment.setAvatar(tokenUserInfoDto.getAvatar());
        videoComment.setNickName(tokenUserInfoDto.getNickName());

        videoCommentService.postComment(videoComment, reqVO.getReplyCommentId());

        return getSuccessResponseVO(videoComment);
    }

    @RequestMapping("/loadComment")
    public ResponseVO loadComment(@Validated WebVideoCommentLoadCommentReqVO reqVO) {

        VideoInfo videoInfo = videoInfoService.getVideoInfoByVideoId(reqVO.getVideoId());
        // 先判断这个视频是否关闭评论
        if (ObjectUtils.isEmpty(videoInfo)) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        if (!StringTools.isEmpty(videoInfo.getInteraction()) && videoInfo.getInteraction().contains("1")) {
            return getSuccessResponseVO(new ArrayList<>());
        }

        VideoCommentQuery videoCommentQuery = new VideoCommentQuery();
        videoCommentQuery.setPageNo(reqVO.getPageNo() == null ? 1 : reqVO.getPageNo());
        videoCommentQuery.setVideoId(reqVO.getVideoId());
        videoCommentQuery.setLoadChildren(true);
        videoCommentQuery.setpCommentId(Constants.ZERO);
        videoCommentQuery.setPageSize(Constants.LENGTH_15);
        videoCommentQuery.setOrderBy(reqVO.getOrderType() == 0 ? "top_type desc,like_count desc, comment_id desc" : "top_type desc,comment_id desc");

        PaginationResultVO<VideoComment> commentData = videoCommentService.findListByPage(videoCommentQuery);
        List<UserAction> userActionList = new ArrayList<>();
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        if (!ObjectUtils.isEmpty(tokenUserInfoDto)) {

            UserActionQuery userActionQuery = new UserActionQuery();
            userActionQuery.setVideoId(reqVO.getVideoId());
            userActionQuery.setUserId(tokenUserInfoDto.getUserId());
            userActionQuery.setActionTypeArray(new Integer[]{UserActionTypeEnum.COMMENT_HATE.getType(),
            UserActionTypeEnum.COMMENT_LIKE.getType()});
            userActionList = userActionService.findListByParam(userActionQuery);
        }

        VideoCommentResultVO vo = new VideoCommentResultVO();
        vo.setCommentData(commentData);
        vo.setUserActionList(userActionList);

        return getSuccessResponseVO(vo);
    }

    /**
     * 置顶评论
     * @return
     */
    @PostMapping("/topComment")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO topComment(@Validated @NotNull Integer commentId) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        VideoComment videoComment = videoCommentService.getVideoCommentByCommentId(commentId);

        if (ObjectUtils.isEmpty(videoComment)) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        if (!Objects.equals(tokenUserInfoDto.getUserId(), videoComment.getVideoUserId())) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }

        /**
         * 置顶这个，将其他的置顶状态改为未置顶
         */
        videoCommentService.updateTopComment(videoComment);

        return getSuccessResponseVO(null);
    }

    /**
     * 取消置顶评论
     * @param commentId
     * @return
     */
    @PostMapping("/cancelTopComment")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO cancelTopComment(@Validated @NotNull Integer commentId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        VideoComment videoComment = videoCommentService.getVideoCommentByCommentId(commentId);

        if (ObjectUtils.isEmpty(videoComment)) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        if (!Objects.equals(tokenUserInfoDto.getUserId(), videoComment.getVideoUserId())) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }

        VideoComment updateComment = new VideoComment();
        updateComment.setTopType(VideoTopTypeEnum.NO_TOP.getCode());
        videoCommentService.updateVideoCommentByCommentId(updateComment, commentId);

        return getSuccessResponseVO(null);
    }

    /**
     * 删除评论
     * @param commentId
     * @return
     */
    @PostMapping("/userDelComment")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO userDelComment(@Validated @NotNull Integer commentId) {
        /**
         * 自己只能删除自己的评论
         * 视频发布者可以删除任何人的评论
         */
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();

        VideoComment videoComment = videoCommentService.getVideoCommentByCommentId(commentId);

        if (ObjectUtils.isEmpty(videoComment)) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        if (!Objects.equals(tokenUserInfoDto.getUserId(), videoComment.getUserId()) || !Objects.equals(tokenUserInfoDto.getUserId(), videoComment.getVideoUserId())) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }

        videoCommentService.deleteVideoCommentByCommentIdOrPcommentId(commentId);

        return getSuccessResponseVO(null);
    }

}
